<?php
/*
 +----------------------------------------------------------------------
 | TEMMOKUMVC [ NO BEST , ONLY BETTER ]
 +----------------------------------------------------------------------
 | Copyright (c) 2018~2019 https://www.temmoku.cn All rights reserved.
 +----------------------------------------------------------------------
 | Author: 张宗强 Email:webmaster@temmoku.cn QQ:158726877 516669373 TEL:17895221001 微信:temmokumvc
 +---------------------------------------------------------------------- 
*/
namespace home\controller;
use \temmoku\controller;
use \temmoku\db;
use \temmoku\lib\pages;
class search extends controller{
	public function index(){
		$keyword = $_POST['keyword'] ? $_POST['keyword'] : $_GET['keyword'];
		!isset($keyword) && $this->err('关键词不可为空');
		$TITLE=$keyword.'_搜索列表_'.C('WEBNAME');
    	//关键字标签
    	$KEYWORDS=C('WEB_SEO_KEYWORDS');
  
    	!isset($_GET['page']) && $_GET['page']=1;
    	$NUM=20;
    	$getlist=db::calcFoundRows()->select('*')->from(jab.'article')->where("title LIKE '%{$keyword}%' AND state=1")->orderByDESC(array('aid'))->setPaging($NUM)->page($_GET['page'])->query();
    	$getlistNUM=db::row(' SELECT FOUND_ROWS() AS allNUM');
		$Crumbs="<a href='".WEBURL."'>首页</a>-<a href='/search/index/keyword/".$keyword.".".C('html')."'>搜索列表</a>";
    	$Get_Page=(new Pages)->Pages('','','',$NUM,$_GET['page'],$getlistNUM['allNUM']);
    	$Get_Page = preg_replace_callback('/page_([0-9]+)/',function ($matches) {
			$keyword = $_POST['keyword'] ? $_POST['keyword'] : $_GET['keyword'];
	    	return WEBURL."/search/index/keyword/".$keyword."/page/".$matches[1].C('html');
    	}, $Get_Page);
    	//不是pc设备才去获取
    	if(DEVICE !='pc'){
			if($_GET['json']==true){
				$this->assign('json',true);
			}
			$_getlist=$getlist;
			unset($data,$getlist,$Get_Page);//防止泄露敏感信息
			foreach($_getlist as $row){
				$_row[url]=U(['aid'=>$row['aid'],'cid'=>$row['cid']]);
				$_row[title]=$row[title];
				$_row[posttime]=date('Y-m-d H:i:s',$row[posttime]);
				$getlist[]=$_row;
			}
			$page=ceil($getlistNUM['allNUM']/$NUM);
			$this->assign('page',$page);
    		$this->assign('webname',$keyword.'搜索列表');
    	}
    	$this->assign('Get_Page',$Get_Page);
    	$this->assign('Crumbs',$Crumbs);
    	$this->assign('getlist',$getlist);
    	$this->assign('keyword',$keyword);
    	$this->assign('WEBNAME',$TITLE);
    	$this->assign('KEYWORDS',$KEYWORDS);
		$this->display(tpl('search'));
	}
}
?>